﻿module LunchCommand {

    export class LunchStatusController {

        private service: LunchStatusService;
        private area: JQuery;
        private afterDecisionElements: JQuery;
        private beforeDecisionElements: JQuery;
        private lunchStatusText: JQuery;
        private mainVenueText: JQuery;
        private quickVenueText: JQuery;
        private quickVenueArea: JQuery;
        private dissentingArea: JQuery;
        private dissentingVenueText: JQuery;
        private mainConsensus: JQuery;
        private quickConsensus: JQuery;
        private dissentingConsensus: JQuery;
        private travelTimeText: JQuery;
        private timeText: JQuery;

        public lastStatus: ILunchStatusModel;

        constructor(area: JQuery) {
            this.area = area;
            this.service = new LunchStatusService();
        }

        public initialize(): void {
            this.afterDecisionElements = this.area.find(".after-decision");
            this.beforeDecisionElements = this.area.find(".before-decision");
            this.lunchStatusText = this.area.find(".lunch-status");
            this.mainVenueText = this.area.find("#mainVenue");
            this.quickVenueText = this.area.find("#quickVenue");
            this.quickVenueArea = this.area.find(".quick-venue");
            this.dissentingArea = this.area.find(".dissenting-venue");
            this.dissentingVenueText = this.area.find("#dissentingVenue");
            this.mainConsensus = this.area.find("#mainConsensus");
            this.quickConsensus = this.area.find("#quickConsensus");
            this.dissentingConsensus = this.area.find("#dissentingConsensus");
            this.timeText = this.area.find("#timeUntilLunch");
            this.travelTimeText = this.area.find("#travelTime");

            this.getStatus();
        }

        private getStatus(): void {
            this.service.GetStatus()
                .done((status: ILunchStatusModel) => this.processStatus(status))
                .fail(failure=> {
                    console.error("Failed to get lunch status");
                    console.info(failure);
                    window.setTimeout(() => { this.getStatus(); }, 60000);
            });
        }

        private processStatus(status: ILunchStatusModel): void {

            this.lastStatus = status;

            this.lunchStatusText.text(status.StatusText);

            var timeout: number = 600000;

            switch (status.Status) {
                case LunchStatus.NotStarted:
                    this.beforeDecisionElements.show();
                    this.afterDecisionElements.hide();

                    timeout = 60000;

                    this.timeText.text(status.TimeRemaining);
                    break;
                case LunchStatus.DidNotHappen:
                    this.beforeDecisionElements.hide();
                    this.afterDecisionElements.hide();
                    break;
                case LunchStatus.StartedWithConsensus:
                case LunchStatus.StartedWithoutConsensus:
                    this.beforeDecisionElements.hide();

                    if (status.DecidedVenue == null) {
                        this.mainVenueText.text("No venues could be determined");
                    } else {
                        this.mainVenueText.text(status.DecidedVenue.Venue.Name);
                        this.mainConsensus.text(status.DecidedVenue.PercentageOfUsers + '%');

                        if (status.MinutesToTravel) {
                            this.travelTimeText.text("Current Travel Time: " + status.MinutesToTravel + " minutes");
                            this.travelTimeText.show();
                        } else {
                            this.travelTimeText.hide();
                        }

                    }

                    if (status.QuickVenue != null && status.QuickVenue.Venue.VenueID !== status.DecidedVenue.Venue.VenueID) {
                        this.quickVenueArea.show();
                        this.quickVenueText.text(status.QuickVenue.Venue.Name);
                        this.quickConsensus.text('(' + status.QuickVenue.PercentageOfUsers + '% Consensus)');
                    } else {
                        this.quickVenueArea.hide();
                    }

                    if (status.DissentingVenue != null) {
                        this.dissentingArea.show();
                        this.dissentingVenueText.text(status.DissentingVenue.Venue.Name);
                        this.dissentingConsensus.text('(' + status.DissentingVenue.PercentageOfUsers + '% Consensus among dissenters)');
                    } else {
                        this.dissentingArea.hide();
                    }

                    this.afterDecisionElements.show();
                    break;
            }

            window.setTimeout(() => { this.getStatus(); }, timeout);
        }
    }
} 